package com.rdc.qingkou.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.adapter.MyFragmentStatePagerAdapter;
import com.rdc.qingkou.base.BaseActivity;
import com.rdc.qingkou.config.Constant;
import com.rdc.qingkou.presenter.PersonalHomepagePresenter;
import com.rdc.qingkou.ui.iview.IPersonalHomepageView;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.view.fragment.PersonalHomepageFragment;
import com.rdc.qingkou.vo.CardVO;
import com.rdc.qingkou.vo.UserVO;
import com.rdc.qingkou.widget.GalleryPageTransformer;
import com.rdc.qingkou.widget.MyViewPager;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class PersonalHomepageActivity extends BaseActivity<PersonalHomepagePresenter> implements IPersonalHomepageView, View.OnClickListener {

    //    private User mUser;
    private MyViewPager mCardViewPager;
    private MyFragmentStatePagerAdapter mAdapter;
    private List<PersonalHomepageFragment> mPersonalHomepageList = new ArrayList<>();
    private TextView mCollectNumTextView;
    private TextView mAppreciateNumTextView;
    private TextView mConcernNumTextView;
    private boolean isLastPage;
    private boolean isUpdate;
    private int mCurrentPage = 1;
    private CircleImageView mHeadCircleView;
    private TextView mNameTextView;
    private TextView mSignNameTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.windowBackground), true);
        setContentView(R.layout.activity_personal_homepage);
        initViews();
        if (MyUtils.isNetworkConnected(this)) {
            mPresenter.getMyUserInfo();
            mPresenter.getStatusNum(MyApplication.getUser().getUserId());
            mPresenter.getCards(MyApplication.getUser().getUserId(), mCurrentPage);
        } else {
            Toast.makeText(this, "无网络！", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public PersonalHomepagePresenter getInstance() {
        return new PersonalHomepagePresenter();
    }

    private void initViews() {
        mHeadCircleView = (CircleImageView) findViewById(R.id.personal_homepage_head_circleImageView);
        mHeadCircleView.setOnClickListener(this);
        mNameTextView = (TextView) findViewById(R.id.personal_homepage_name_textView);
        mNameTextView.setOnClickListener(this);
        mSignNameTextView = (TextView) findViewById(R.id.personal_homepage_signName_textView);
        mSignNameTextView.setOnClickListener(this);
        mCollectNumTextView = (TextView) findViewById(R.id.personal_homepage_collect_textView);
        mAppreciateNumTextView = (TextView) findViewById(R.id.personal_homepage_appreciate_textView);
        mConcernNumTextView = (TextView) findViewById(R.id.personal_homepage_concern_textView);
        mCollectNumTextView.setOnClickListener(this);
        mAppreciateNumTextView.setOnClickListener(this);
        mConcernNumTextView.setOnClickListener(this);
        findViewById(R.id.personal_homepage_appreciate_imageButton).setOnClickListener(this);
        findViewById(R.id.personal_homepage_collect_imageButton).setOnClickListener(this);
        findViewById(R.id.personal_homepage_concern_imageButton).setOnClickListener(this);
        findViewById(R.id.personal_homepage_back_imageButton).setOnClickListener(this);
        findViewById(R.id.personal_homepage_message_ImageButton).setOnClickListener(this);

        mCardViewPager = (MyViewPager) findViewById(R.id.personal_homepage_viewpager);
        mAdapter = new MyFragmentStatePagerAdapter<>(getSupportFragmentManager(), mPersonalHomepageList);
        mCardViewPager.setAdapter(mAdapter);
        mCardViewPager.setOffscreenPageLimit(3);
        mCardViewPager.setPageTransformer(false, new GalleryPageTransformer());
        mCardViewPager.setPageMargin(-80);
        mCardViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if ((position == mAdapter.getCount() - 1) && !isLastPage && !isUpdate) {
                    //到最后一页自动加载更多
                    if (MyUtils.isNetworkConnected(PersonalHomepageActivity.this)) {
                        mPresenter.getCards(MyApplication.getUser().getUserId(), mCurrentPage);
                    } else {
                        Toast.makeText(PersonalHomepageActivity.this, "无网络！", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.personal_homepage_back_imageButton:
                onBackPressed();
                break;
            case R.id.personal_homepage_message_ImageButton:
                Intent intent = new Intent(this, MessageActivity.class);
                startActivity(intent);
                break;
            case R.id.personal_homepage_head_circleImageView:
            case R.id.personal_homepage_name_textView:
            case R.id.personal_homepage_signName_textView:
                Intent intent1 = new Intent(this, PersonalSettingActivity.class);
                startActivity(intent1);
                break;
            case R.id.personal_homepage_appreciate_imageButton:
            case R.id.personal_homepage_appreciate_textView:
                Intent intent2 = new Intent(this, BrowseActivity.class);
                intent2.putExtra("appreciate", 1);
                startActivity(intent2);
                break;
            case R.id.personal_homepage_collect_imageButton:
            case R.id.personal_homepage_collect_textView:
                Intent intent3 = new Intent(this, BrowseActivity.class);
                intent3.putExtra("collect", 1);
                startActivity(intent3);
                break;
            case R.id.personal_homepage_concern_imageButton:
            case R.id.personal_homepage_concern_textView:
                Intent intent4 = new Intent(this, SubscribeActivity.class);
                startActivity(intent4);
                break;
        }
    }

    @Override
    public void setStatusNum(int praiseNum, int collectNum, int subscribeNum) {
        mAppreciateNumTextView.setText(String.valueOf(praiseNum));
        mCollectNumTextView.setText(String.valueOf(collectNum));
        mConcernNumTextView.setText(String.valueOf(subscribeNum));
    }

    @Override
    public void setCards(List<CardVO> cards) {
        if (cards != null) {
            if (cards.size() < Constant.CARD_NUM_OF_ONEPAGE) {
                isLastPage = true;
            } else {
                mCurrentPage++;
            }
            for (CardVO card : cards) {
                PersonalHomepageFragment ohf = new PersonalHomepageFragment();
                ohf.setCard(card);
                mPersonalHomepageList.add(ohf);
            }
            mAdapter.notifyDataSetChanged(true);
        } else {
            isLastPage = true;
        }
        isUpdate = false;
    }

    @Override
    public void getCardError(String message) {
        isUpdate = false;
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getStatusNumError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setMyUserInfo(UserVO userVO) {
        mNameTextView.setText(userVO.getNickName());
        mSignNameTextView.setText(userVO.getSignature());
        Glide.with(this).load(userVO.getHeadImageUrl()).into(mHeadCircleView);
    }


    @Override
    public void getMyUserInfoError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 2:
                if (resultCode == BrowseDetailActivity.DELETED) {
                    int deletedCardId = data.getIntExtra("deletedCardId", 0);
                    for (int i = 0; i < mPersonalHomepageList.size(); i++) {
                        CardVO cardVO = mPersonalHomepageList.get(i).getCard();
                        if (cardVO.getCardId() == deletedCardId) {
                            mPersonalHomepageList.remove(i);
                            mAdapter.notifyDataSetChanged(true);
                            break;
                        }
                    }
                }
                break;
        }
    }
}
